package com.dg.river.core.util;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;

/**
 * @作者：HuYong
 * @时间：2021/11/6 16:47
 * @描述：定位工具类
 * @修改：
 */
public class LocationUtils {

    private static LocationUtils locationUtils;

    public static LocationUtils getInstance() {
        if (locationUtils == null) {
            synchronized (LocationUtils.class) {
                if (locationUtils == null) {
                    locationUtils = new LocationUtils();
                }
            }
        }
        return locationUtils;
    }

    public interface LocationCallBack {

        // 定位的全部信息
        void locationInfo(AMapLocation aMapLocation);

        // 定位失败
        void locationError();
    }

    /**
     * 调用此方法实现获取定位信息
     */
    public void getLocation(Context mContext, LocationCallBack locationCallBack) {
        if (!PermissionsCheckerUtils.checkLocationPermission(mContext)) {
            if (locationCallBack != null) {
                locationCallBack.locationError();
            }
        } else {
            try {
                AMapLocationClient.updatePrivacyShow(mContext, true, true);
                AMapLocationClient.updatePrivacyAgree(mContext, true);
                AMapLocationClient mLocationClient = new AMapLocationClient(mContext);
                mLocationClient.setLocationListener(aMapLocation -> {
                    if (aMapLocation != null) {
                        if (aMapLocation.getErrorCode() == 0) {
                            if (locationCallBack != null) {
                                locationCallBack.locationInfo(aMapLocation);
                            }
                        } else {
                            if (locationCallBack != null) {
                                locationCallBack.locationError();
                            }
                        }
                    } else {
                        if (locationCallBack != null) {
                            locationCallBack.locationError();
                        }
                    }
                });
                // 配置设置
                mLocationClient.setLocationOption(initOption());
                // 设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
                mLocationClient.stopLocation();
                mLocationClient.startLocation();
            } catch (Exception e) {
                e.printStackTrace();
                locationCallBack.locationError();
            }
        }
    }

    /**
     * 添加设置
     *
     * @return
     */
    private AMapLocationClientOption initOption() {
        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        option.setNeedAddress(true);
        option.setMockEnable(false);
        return option;
    }
}
